package org.geektimes.reactive.streams.simple;

import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

/**
 * @FileName: DefaultSubscriber
 * @Description: TODO
 * @Author: MaWenHao
 * @Created: 2021/3/30 - 下午11:58
 */
public class DefaultSubscriber<T> implements Subscriber<T> {

    private Subscription subscription;

    private int count;

    @Override
    public void onSubscribe(Subscription s) {
        this.subscription = s;
    }

    @Override
    public void onNext(T t) {
        System.out.println("接收数据:" + t);
        count ++;
        if (count > 2) {
            subscription.cancel();
        }
    }

    @Override
    public void onError(Throwable t) {
        System.out.println("发生异常:" + t);
    }

    @Override
    public void onComplete() {
        System.out.println("接收数据结束");
    }
}
